package com.hoongs.v1.entity;

import com.baomidou.mybatisplus.annotation.*;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.hoongs.v1.utils.DateUtil;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Repository;

import javax.validation.constraints.NotBlank;
import java.math.BigDecimal;
import java.time.LocalDateTime;

@Data
@NoArgsConstructor
@AllArgsConstructor
@Repository
@TableName(value = "rider")
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonIgnoreProperties(value = {"createTime", "updateTime"})
public class Rider {

    @TableId(type = IdType.ASSIGN_UUID)
    private String id;

    @TableField("user_id")
    private String userId;     // 关联用户表ID

    private String image;      // 骑手照片

    @NotBlank(message = "请输入骑手名称")
    private String name;

    private String age; // 年龄

    private String gender = "0"; // 性别信息 1:男 2:女 0:保密

    @NotBlank(message = "请输入骑手联系方式")
    private String mobile;

    private String status = "0";         // 用户状态 0:不可用 1:正常

    @TableField("is_work")
    private String isWork = "0";         // 是否开工 0:开工 1:下班

    private String rate = "5";

    private String summary;         // 简介

    private String remark;         // 备注

    private String sortcode;         // 备注

    @TableField("order_num")
    private BigDecimal orderNum;

    @JsonFormat(pattern = DateUtil.PATTERN_DATETIME)
    @TableField(value = "create_time", fill = FieldFill.INSERT)
    private LocalDateTime createTime;       // 发布时间

    @JsonFormat(pattern = DateUtil.PATTERN_DATETIME)
    @DateTimeFormat(pattern = DateUtil.PATTERN_DATETIME)
    @TableField(value = "update_time", fill = FieldFill.INSERT_UPDATE)
    private LocalDateTime updateTime;    // 更新时间

    /**
     * 用骑手信息生成账户
     * 密码是手机号后六位
     *
     * @param rider
     * @return
     */
    public static User toUser(Rider rider) {
        User user = new User();
        user.setId(rider.getUserId());
        user.setUsername(rider.getMobile());
        String pwd = "bk@r" + rider.getMobile().substring(7);
        user.setPassword(new BCryptPasswordEncoder().encode(pwd));
        user.setStatus(rider.getStatus());
        user.setRole("8");
        user.setMobile(rider.getMobile());
        user.setGender(rider.getGender());
        user.setSummary("【骑手】" + rider.getSummary());
        return user;
    }
}
